//
//  XMOAuthController.m
//  XMWeibo
//
//  Created by chi on 14-8-22.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMOAuthController.h"

#import "MBProgressHUD+XM.h"
#import "XMWeiboAccount.h"
#import "XMHttpTool.h"

#import "XMWeiboTool.h"

@interface XMOAuthController () <UIWebViewDelegate>

@property (nonatomic, weak) UIWebView *webView;

@end

@implementation XMOAuthController



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    // 创建webview
    [self setupWebView];


}


#pragma mark - 创建webview
- (void)setupWebView
{

    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    
    self.webView = webView;
    webView.delegate = self;
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:k_oauth2_authorize]];
    
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
    
    
    UILabel *label = [[UILabel alloc]init];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:12.0];
    label.textColor = [UIColor greenColor];
    label.lineBreakMode = NSLineBreakByCharWrapping;
    label.backgroundColor = [UIColor blackColor];
    label.text = @"帐号:hack_mac@qq.com\n密码:chi123456\n其它帐号需要添加测试权限";
    CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(240.0, MAXFLOAT)];
    
    if (isIOS7) {
        label.frame = (CGRect){CGPointMake(120, 75), size};
    }
    else {
        label.frame = (CGRect){CGPointMake(120, 15), size};
    }
    [self.view addSubview:label];

    
}


#pragma mark - 获取token
- (void)getAccess_token:(NSString*)code
{
    [MBProgressHUD hideHUD];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    dict[@"client_id"] = k_client_id;
    dict[@"client_secret"] = k_client_secret;
    dict[@"grant_type"] = @"authorization_code";
    dict[@"code"] = code;
    dict[@"redirect_uri"] = k_redirect_uri;
    
    [XMHttpTool httpPostWithURL:k_oauth2_access_token params:dict success:^(id responseObject) {
        // 实例化帐户
        [XMWeiboAccount weiboAccountWithDictionary:responseObject];
        
        // 归档
        [XMWeiboAccount saveAccount];
        
        // 选择控制
        [XMWeiboTool chooseRootViewController];
    } failure:^(NSError *error) {
         XMLog(@"AFN出错，错误原因: %@", error);
    }];
    
    
}

- (void)dealloc
{
    [MBProgressHUD hideHUD];
}



#pragma mark - uiwebview代理

/**
 *  在加载页面前被调用
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *requesturl = request.URL.absoluteString;
    
    NSRange strRange = [requesturl rangeOfString:[k_redirect_uri stringByAppendingString:@"?code="]];
    if (strRange.location != NSNotFound) {
        NSString *code = [requesturl substringFromIndex:strRange.length];
        //获取token
        [self performSelector:@selector(getAccess_token:) withObject:code afterDelay:0.5f];
//        [self getAccess_token:code];
        
        [MBProgressHUD hideHUD];
        return NO;
    }
    
    
    return YES;
}

/**
 *  开始加载页面
 */
- (void)webViewDidStartLoad:(UIWebView *)webView
{

    [MBProgressHUD showMessage:@"哥正在拼命加载中..."];
}

/**
 *  完成加载页面
 */
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUD];
}

@end
